
#include "LengthUnit.h"
#include <sstream>

const unsigned int FEET_TO_FEET(1);
const unsigned int YARD_TO_FEET(3);
const unsigned int MILE_TO_FEET(1760 * YARD_TO_FEET);

const UnitType  UnitTypeLength;
const LengthUnit MILE(MILE_TO_FEET, UnitTypeLength);
const LengthUnit YARD(YARD_TO_FEET, UnitTypeLength);
const LengthUnit FEET(FEET_TO_FEET, UnitTypeLength);

LengthUnit::LengthUnit(unsigned int unit, const UnitType &unitType)
:
Unit(unit, unitType)
{
}

string LengthUnit::formatToString(unsigned int amount) const
{
	unsigned int amoutInBaseUnit = getValueInBaseUnit(amount);

	if(0 == amoutInBaseUnit)
	{
		return string("0 FEET");
	}
	unsigned int mile = amoutInBaseUnit / MILE_TO_FEET;
	stringstream tmpStr;
	if(0 != mile)
	{
		tmpStr << mile <<" MILE";
	}
	unsigned int yard = amoutInBaseUnit % MILE_TO_FEET / YARD_TO_FEET;
	if(0 != yard)
	{
        if(0 != mile)
        {
            tmpStr << " ";
        }
		tmpStr << yard << " YARD";
	}
	unsigned int feet = amoutInBaseUnit % YARD_TO_FEET;
	if(0 != feet)
	{
        if(0 != mile || 0 != yard)
        {
            tmpStr << " ";
        }
		tmpStr << feet << " FEET";
	}
	return tmpStr.str();
}
string LengthUnit::getBaseUnitName() const
{
    return string("FEET");
}


